package nayasis.common;

import java.text.ParseException;

import junit.framework.Assert;
import nayasis.common.util.NDate;

import org.junit.Test;

public class DateTest {

	/**
	 * 생성자 테스트
	 * 
	 * @throws ParseException
	 */
	@Test
	public void makeConstructor() throws ParseException {

		NDate d1 = new NDate( "2012-12-20 01:26:55.104", "YYYY-MM-DD HH:MI:SS.FFF");
		
		Assert.assertEquals( d1.toString("YYYY.MM.DD HH:MI:SS.FFF"), "2012.12.20 01:26:55.104" );

		NDate d2 = new NDate( "2012.02.29 13:21:42" );

		Assert.assertEquals( d2.toString(), "2012.02.29 13:21:42" );

	}

	/**
	 * 날짜 연산 테스트
	 *
	 * @throws ParseException
	 */
	@Test
	public void testAddOperation() throws ParseException {

		NDate date = new NDate( "2012.02.29 13:21:42" );

		date.addYear( 1 );

		Assert.assertEquals( date.toString(), "2013.02.28 13:21:42" );

		date.addMonth( 1 );
		Assert.assertEquals( date.toString(), "2013.03.28 13:21:42" );

		date.addDate( 10 );
		Assert.assertEquals( date.toString(), "2013.04.07 13:21:42" );

	}

	/**
	 * 월초/월말 계산 테스트
	 *
	 * @throws ParseException
	 */
	@Test
	public void testMonthStartAndEnd() throws ParseException {

		NDate date = new NDate( "2012.02.29" );

		Assert.assertEquals( date.getFirstMonthDate().toString(), "2012.02.01 00:00:00" );
		Assert.assertEquals( date.getLastMonthDate().toString(),  "2012.02.29 00:00:00" );

	}

	/**
	 * 날짜간 차이 계산
	 *
	 * @throws ParseException
	 */
	@Test
	public void testBetweenDate() throws ParseException {

		NDate d1 = new NDate( "2012.02.29 14:20" );
		NDate d2 = new NDate( "2012.03.01 00:00" );

		Assert.assertEquals( d1.getBetweenDays(d2), 1 );
		Assert.assertEquals( d1.getBetweenHour(d2), 9 );
		Assert.assertEquals( d1.compareTo(d2), -1 );

	}

	@Test
	public void hourTest() throws ParseException {
		
		NDate hour = new NDate( "00:00:00", "HH:MI:SS" );
		
		hour.addHour( 10 );
		hour.addMinute( 13 );
		hour.addSecond( 55 );
		
		Assert.assertEquals( "10:13", hour.toString("HH:MI") );
		
		hour.addSecond( 15 );
		Assert.assertEquals( "10:14", hour.toString("HH:MI") );

	}

	@Test
	public void compare() throws ParseException {

        NDate d1 = new NDate( "2012.02.29 14:20" );
        NDate d2 = new NDate( "2012.03.01 00:00" );
        
        Assert.assertEquals( false, d1.greaterThan(d2) );
        Assert.assertEquals( false, d1.greaterThanOrEqual(d2) );
        Assert.assertEquals( true, d1.lessThan(d2) );
        Assert.assertEquals( true, d1.lessThanOrEqual(d2) );
	    
	}
	
}
